package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	DatabaseTest dt;
	private JLabel lblNewLabel;
	private JLabel lblCode;
	private JLabel lblEa;
	private JTextField serial;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	

	/**
	 * Create the frame.
	 */
	public Delete(Connection c,DatabaseTest d) {
		super("삭제",true,true,true,false);
		setBounds(100, 100, 450, 300);
		this.conn = c;
		this.dt = d;
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblCode());
		getContentPane().add(getLblEa());
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(12, 10, 76, 25);
		}
		return lblNewLabel;
	}
	private JLabel getLblCode() {
		if (lblCode == null) {
			lblCode = new JLabel("code");
			lblCode.setBounds(12, 41, 76, 25);
		}
		return lblCode;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(12, 72, 76, 25);
		}
		return lblEa;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(99, 10, 200, 25);
			serial.setColumns(10);
		}
		return serial;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(100, 41, 200, 25);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(99, 72, 200, 25);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
					try{
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							System.out.println("데이터가 없습니다.");
						}
						rs.close();
						pstmt.close();
						
					}catch(Exception ex){
						ex.printStackTrace();
					
				}
				}
			});
			btnNewButton.setBounds(311, 11, 97, 23);
		}
		return btnNewButton;
	}
	
	public void setDelete(String s,String c,String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
		
	}
	
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String sql = "delete ptbl where serial = ?";
					
					try{
					PreparedStatement pstmt = conn.prepareStatement(sql);
					pstmt.setInt(1, s);
					pstmt.executeUpdate();
					
					pstmt.close();	
					if(dt.select != null)dt.select.setList();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_1.setBounds(312, 44, 97, 23);
		}
		return btnNewButton_1;
	}
}
